package com.lyhengtongwl.putorefresh.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lyhengtongwl.putorefresh.R;
import com.lyhengtongwl.putorefresh.SPUtil;

import java.text.SimpleDateFormat;
import java.util.Objects;


public class HeadRefreshView extends FrameLayout implements HeadView {
    private TextView tv;
    private ImageView arrow;
    private ProgressBar progressBar;
    private TextView tv_time;
    private Context context;
    private String time="";
    public HeadRefreshView(Context context) {
        this(context, null);
    }

    public HeadRefreshView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeadRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_header, null);
        addView(view);
        tv = (TextView) view.findViewById(R.id.header_tv);
        tv_time = (TextView) view.findViewById(R.id.tv_time);
        arrow = (ImageView) view.findViewById(R.id.header_arrow);
        progressBar = (ProgressBar) view.findViewById(R.id.header_progress);
    }

    @Override
    public void begin() {

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void progress(float progress, float all) {
        time= Objects.requireNonNull(SPUtil.get(context, "lastTime", "")).toString();
        if ("".equals(time)){
            time=getLastTime(System.currentTimeMillis());
        }
        float s = progress / all;
        if (s >= 0.9f) {
            arrow.setRotation(180);
        } else {
            arrow.setRotation(0);
        }
        if (progress >= all - 10) {
            tv.setText("松开刷新");
            tv_time.setText("最近更新时间："+time);
        } else {
            tv.setText("下拉刷新");
            tv_time.setText("最近更新时间："+time);
        }
    }

    @Override
    public void finishing(float progress, float all) {
        SPUtil.put(context,"lastTime",getLastTime(System.currentTimeMillis()));
    }

    @Override
    public void loading() {
        arrow.setVisibility(GONE);
        progressBar.setVisibility(VISIBLE);
        tv.setText("刷新中...");
    }

    @Override
    public void normal() {
        arrow.setVisibility(VISIBLE);
        progressBar.setVisibility(GONE);
        tv.setText("下拉刷新");
    }

    @Override
    public View getView() {
        return this;
    }
    //时间转换  返回格式：00:00:00
    public static String getLastTime(long time) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");//这里想要只保留分秒可以写成"mm:ss"
            return formatter.format(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "0";
    }
}
